//
//  UIButton+AnimationBtn.m
//  VDLoading
//
//  Created by wsj2015 on 2017/3/6.
//  Copyright © 2017年 VD. All rights reserved.
//

#import "UIButton+AnimationBtn.h"
#import <objc/runtime.h>



@implementation UIButton (AnimationBtn)

@dynamic evnetblock;

@dynamic endAniblock;



- (void)setFrame:(CGRect)frame{

    [super setFrame:frame];
   

}

- (void)setEvnetblock:(EnventBlock)evnetblock{
    objc_setAssociatedObject(self, "EnventBlock", evnetblock, OBJC_ASSOCIATION_RETAIN);
    objc_setAssociatedObject(self, "Ori_Frame", [NSValue valueWithCGRect:self.frame], OBJC_ASSOCIATION_RETAIN);
    [self addTarget:self action:@selector(loginButtonDidClick) forControlEvents:UIControlEventTouchUpInside];
  
}

// 开始动画
- (void)loginButtonDidClick {
    
    //禁用用户交互
    self.userInteractionEnabled = NO;
    //隐藏title
//    [self setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
   
    //执行代码块
    EnventBlock envent =objc_getAssociatedObject(self, "EnventBlock");
    envent();
    
    //开始动画
    [UIView animateWithDuration:1.0 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:0.8 options:UIViewAnimationOptionLayoutSubviews animations:^{
         self.titleLabel.transform = CGAffineTransformScale(self.titleLabel.transform, 0, 0);
        //1.将矩形按钮缩成圆型
        //改变圆角
        self.layer.cornerRadius = self.frame.size.height / 2.0;
        self.layer.masksToBounds = YES;
        //改变frame
        self.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width - self.frame.size.height) / 2.0, self.frame.origin.y, self.frame.size.height, self.frame.size.height);
    } completion:^(BOOL finished) {
        
        //添加小弧线
        [self.layer addSublayer:self.leftShape];
        //显示小弧线
        self.leftShape.hidden = NO;
        
        //2.旋转圆形按钮
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        
        animation.toValue = @(M_PI * 7000);
        animation.duration = 1000;
        animation.beginTime = 0;
        animation.fillMode = kCAFillModeBoth;
        [self.layer addAnimation:animation forKey:@"rotation"];
    }];
}



//结束动画
- (void)setEndAniblock:(EndAnimaitonBlock)endAniblock{

    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSValue  *deframe_value =  objc_getAssociatedObject(self, "Ori_Frame");
        CGRect deframe = [deframe_value CGRectValue];
        //移除旋转动画
//       CAAnimation *animation = [self.layer animationForKey:@"rotation"];
        [self.layer removeAnimationForKey:@"rotation"];
        
        //隐藏小弧线
        self.leftShape.hidden = YES;
        
        //开始动画
        [UIView animateWithDuration:1.0 delay:0.2 usingSpringWithDamping:0.8 initialSpringVelocity:0.8 options:UIViewAnimationOptionLayoutSubviews animations:^{
            
            //3.将圆形按钮展开成矩形
            //恢复最初圆角
            self.layer.cornerRadius = 0.001;
            self.layer.masksToBounds = YES;
            //恢复最初frame
            self.frame = deframe;
            //显示title
            //            [self setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            self.titleLabel.transform =CGAffineTransformIdentity;
        } completion:^(BOOL finished) {
            
            //开启用户交互
            self.userInteractionEnabled = YES;
            //执行代码块
            if (endAniblock) {
                endAniblock();
            }
            
        }];

        
    });
   
    

    
}




- (CAShapeLayer *)leftShape {//白色小弧线
    
    CAShapeLayer *leftShape = objc_getAssociatedObject(self, "leftShape");
    if (!leftShape) {
        leftShape = [[CAShapeLayer alloc]init];
        CGFloat radius = self.frame.size.height / 2.0;
        UIBezierPath *leftPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(radius, radius) radius:radius - 5 startAngle:M_PI_2 + M_PI_4 endAngle:-M_PI clockwise:YES];
        leftShape.lineWidth = 3;
        leftShape.path = leftPath.CGPath;
        leftShape.backgroundColor = [UIColor clearColor].CGColor;
        leftShape.fillColor = [UIColor clearColor].CGColor;
        leftShape.lineCap = kCALineCapRound;
        leftShape.lineJoin = kCALineJoinRound;
        leftShape.strokeColor = [UIColor whiteColor].CGColor;
        leftShape.lineWidth = leftPath.lineWidth;
        objc_setAssociatedObject(self, "leftShape", leftShape, OBJC_ASSOCIATION_RETAIN);
    }
    
 

    return leftShape;
}



@end
